package net.xujialiang.XTestRPA.Service.Tenant;

import net.xujialiang.XTestRPA.Entity.Tenant.XRPATenant;
import net.xujialiang.XTestRPA.Repository.Env.EnvRepository;
import net.xujialiang.XTestRPA.Repository.Tenant.TenantRepository;
import net.xujialiang.XTestRPA.Repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class XRPATenantService {
    @Autowired
    TenantRepository tenantRepository;
    @Autowired
    UserRepository userRepository;
    @Autowired
    EnvRepository envRepository;

    /**
     * 新建租户
     * @param ttc 租户号
     * @param admin 租户管理员
     * @throws Exception
     */
    public void createTenant(String ttc, String admin) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            XRPATenant entity = new XRPATenant();
            entity.setTtc(ttc);
            entity.setUserLimit(1);
            entity.setAdmin(admin);
            tenantRepository.save(entity);
        }else{
            throw new Exception("租户已存在");
        }
    }

    /**
     * 判断是否是租户管理员
     * @param ttc 租户号
     * @param username 用户名
     * @return
     */
    public Boolean isTenantAdmin(String ttc, String username) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            throw new Exception("租户不存在");
        }else{
            if(tenant.get().getAdmin().equals(username)){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 租户信息
     * @param ttc 租户号
     * @return
     * @throws Exception
     */
    public XRPATenant fetchTenantInfo(String ttc) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            throw new Exception("租户不存在");
        }else{
            XRPATenant entity = tenant.get();
            long usedEnv = envRepository.countByTtc(ttc);
            long usedUser = userRepository.countByTtc(ttc);
            entity.setUsedEnv(String.valueOf(usedEnv));
            entity.setUsedUser(String.valueOf(usedUser));
            return entity;
        }
    }

    /**
     * 更新套餐
     * @param ttc
     * @param plan
     * @throws Exception
     */
    public void updatePlan(String ttc, String plan) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            throw new Exception("租户不存在");
        }else{
            XRPATenant entity = tenant.get();
            entity.setPlan(plan);
            tenantRepository.save(entity);
        }
    }

    /**
     * 兑换RPA点数
     * @param ttc 租户号
     * @param points 点数
     * @throws Exception
     */
    public void redeemPoint(String ttc, Integer points) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            throw new Exception("租户不存在");
        }else{
            Integer cost = points/100;
            XRPATenant entity = tenant.get();
            Integer total = entity.getBalance()+entity.getGift();
            if(total < cost) {
                throw new Exception("余额不足");
            }
            if(entity.getGift() >= cost){
                entity.setGift(entity.getGift() - cost);
            }else if(entity.getGift()<cost){
                entity.setGift(0);
                Integer toSub = cost - entity.getGift();
                entity.setBalance(entity.getBalance() - toSub);
            }
            entity.setRpapoints(entity.getRpapoints()+points);
            tenantRepository.save(entity);
        }
    }

    public void costPoints(int points, String ttc) throws Exception {
        Optional<XRPATenant> tenant = tenantRepository.findByTtc(ttc);
        if(tenant.isEmpty()) {
            throw new Exception("租户不存在");
        }else{
            XRPATenant entity = tenant.get();
            long newPoints = entity.getRpapoints() - points;
            entity.setRpapoints(newPoints);
            tenantRepository.save(entity);
        }
    }

}
